/*
 * KKRibbon.h
 *
 *  Created on: 2013-9-23
 *      Author: wanghua
 */

#ifndef KKRIBBON_H_
#define KKRIBBON_H_

/*#include <GLES/egl.h>*/
#include "kaka_common.h"
#include "cocos2d.h"
#include "CCProtocols.h"

USING_NS_CC;

class KKRibbonSegment;

class  KKRibbon : public CCNode, public CCTextureProtocol
{
	/** Texture used by the ribbon. Conforms to CCTextureProtocol protocol */
	CC_PROPERTY(CCTexture2D*, m_pTexture, Texture)
	/** Texture lengths in pixels */
	CC_PROPERTY(float, m_fTextureLength, TextureLength)
	/** GL blendind function */
	CC_PROPERTY(ccBlendFunc, m_tBlendFunc, BlendFunc)
	/** color used by the Ribbon (RGBA) */
	CC_PROPERTY_PASS_BY_REF(ccColor4B, m_tColor, Color)

	CC_SYNTHESIZE(CCTexture2D*, m_pCaptionTexture, CaptionTexture)

	CC_SYNTHESIZE(bool, m_startCaptionEnabled, StartCaptionEnabled)

public:
    KKRibbon() : m_pTexture(0), m_pSegments(0), m_pDeletedSegments(0),m_startCaptionEnabled(true),m_nPointCount(0){}
	virtual ~KKRibbon();

	/** creates the ribbon */
	static KKRibbon * ribbonWithWidth(float w, const char *path, float length, const ccColor4B& color, float fade);
	/** init the ribbon */
	bool initWithWidth(float w, const char *path, float length, const ccColor4B& color, float fade);
	/** add a point to the ribbon */
	void addPointAt(CCPoint location, float width);
	/** polling function */
	void update(ccTime delta);
	/** determine side of line */
	float sideOfLine(const CCPoint& p, const CCPoint& l1, const CCPoint& l2);
	// super method
	virtual void draw();
private:
	/** rotates a point around 0, 0 */
	CCPoint rotatePoint(const CCPoint& vec, float rotation);
protected:
	KKMutableArray<KKRibbonSegment*> *m_pSegments;
	KKMutableArray<KKRibbonSegment*> *m_pDeletedSegments;

	CCPoint	m_tLastPoint1;
	CCPoint	m_tLastPoint2;
	CCPoint	m_tLastLocation;
// 	int		m_nVertCount_;
	float	m_fTexVPos;
	float	m_fCurTime;
	float	m_fFadeTime;
	float	m_fDelta;
	float	m_fLastWidth;
	float	m_fLastSign;
	bool	m_bPastFirstPoint;
	int		m_nPointCount;

	GLfloat	m_pStartCaptionVerts[4*6];
	GLfloat	m_pEndCaptionVerts[4*6];
	GLfloat	m_pStartCoords[4*4];
	GLfloat	m_pEndCoords[4*4];
};

/** @brief object to hold ribbon segment data */
class  KKRibbonSegment : public CCObject
{
public:
	GLfloat	m_pAntialiasVerts[50*6];
	GLfloat	m_pVerts[50*6];
	GLfloat	m_pCoords[50*4];
	GLubyte	m_pColors[50*8];
	float	m_pCreationTime[50];
	bool	m_bFinished;
	unsigned int	m_uEnd;
	unsigned int	m_uBegin;
public:
	KKRibbonSegment()
		: m_bFinished(false)
        , m_uEnd(0)
		, m_uBegin(0)
	{}
	virtual ~KKRibbonSegment();
	char * description();
	bool init();
	void reset();
	void draw(float curTime, float fadeTime, const ccColor4B& color);
};



#endif /* KKRIBBON_H_ */
